/**
 * @desc: vuex
 * @author zhangqiang
 * @date 2022/2/8
 */
import Vue from "vue";
import "babel-polyfill";
// 1. 导入并绑定Vuex
import Vuex from "vuex";
Vue.use(Vuex);
import { getSessionStorageLanguage,setSessionStorageLanguage } from "@/assets/common/js/global";//引入清除缓存
import i18n from '../lang'
// 2. 定义数据存储仓库state
const state = {
    username: "",
    //路由菜单
    routers: "",
    //  当前url的一级菜单url
    level1Url: '',
    // 用户信息
    userInfo: "",
    // 确定左侧菜单栏折叠收起
    collapseWidth: 1,
    screenWidth: document.documentElement.clientWidth,
    // 全局风格代码
    themeCode: '1',
    // 表格空态页图片路径
    noDataUrl: '',
    // 图片空态路径
    noImgUrl: 'static/img/noImgPic.png',
    btnPower: "", // 当前页面按钮权限
    // 登录页，首页 logo 图片
    logoImg: 'static/img/logo1.png',
    // 记录菜单折叠收起按钮点击次数, 用于解决在首次展开菜单，出现横向滚动条问题
    showMenu: 0,
    // 头部菜单数据
    tagsList: [],
    //  是否隐藏视频
    isHideVideo: false,
    // 语言
    language: getSessionStorageLanguage(),
    idCard:"",
    image:"",
    name:"",
    sex:"",
    addDeviceList:[],
    addDeviceListLoading:false,
    nfcCode:"",
    publicConfig:null,
    isAddPeoplePage:false,
    dataDashboard:new Date().getTime(),
    biodataInfos:{
        face:[],
        eye_left:[],
        eye_right:[],
        hand_left:[],
        hand_right:[],
        palmprint_left:[],
        palmprint_right:[],
        fingerprint1:[],
        fingerprint2:[],
        fingerprint3:[],
        fingerprint4:[],
        fingerprint5:[],
        fingerprint6:[],
        fingerprint7:[],
        fingerprint8:[],
        fingerprint9:[],
        fingerprint10:[],
        QRCode:[],
    }
};
// 3. 定义用于修改数据的方法
const mutations = {
    resetBiodataInfos:(state,data)=>{
        for (var item in state.biodataInfos){
            state.biodataInfos[item].splice(0,1)
        }
        console.log('state重置biodataInfos后的数据：',state.biodataInfos);
    },
    updateBiodataInfos:(state,data)=>{
        // console.log('修改的值', data.value);
        state.biodataInfos[data.key][0][data.attributeName] = data.value

    },
    setbiodataInfos:(state,data)=>{
        console.log('在修改biodataInfos',state.biodataInfos[data.key],data);
        if (data.value===null) {
            state.biodataInfos[data.key].splice(0,1)
        }else{
            state.biodataInfos[data.key].splice(0,1,data.value)
        }

        // state.biodataInfos[data.key] = data.value
        // console.log('在修改biodataInfos',data,state.biodataInfos[data.key]);
    },
    updateDataDashboard:(state,value)=>{
        state.dataDashboard = value
    },
    setisAddPeoplePage:(state,value)=>{
        console.log('修改addPeoplePage');
        state.isAddPeoplePage = value
    },
    setPublicConfig:(state,value)=>{
        console.log('修改publicConfig');
        state.publicConfig = value
    },
    setNfcCode:(state,value)=>{
        console.log('vuex修改setNfcCode');
        state.nfcCode = value
    },

    setAddDeviceListLoading:(state,value)=>{
        state.addDeviceListLoading=value
    },
    setAddDeviceList:(state,row)=>{
        // 只添加deviceType!=-1的设备
        if (row.deviceType!=-1){
            var newIp=row.ip,
                index=-1
            // 设备不可用时，把绑定状态设置为未知
            if (!row.available){
                row.isBindding=100
            }
            console.log('新设备的IP是',newIp);

            for (var i=0;i<state.addDeviceList.length;i++){
                if (state.addDeviceList[i].ip==newIp){
                    index=state.addDeviceList[i].ip
                    break
                }
            }
            console.log('根据IP查找的结果是',index);
            if (index===-1){
                state.addDeviceList.push(row)
                console.log(JSON.stringify(state.addDeviceList));
            }
        }

    },
    resetAddDeviceList:(state,value)=>{
        state.addDeviceList=value
    },
    setIdCard:(state,value)=>{
        state.idCard = value
    },
    setImage:(state,value)=>{
        state.image = value
    },
    setName:(state,value)=>{
        state.name = value
    },
    setSex:(state,value)=>{
        state.sex = value
    },
    setRouters: (state, routers) => {
        state.routers = routers;
    },
    // 设置当前登录用户信息
    setUserInfo: (state, userInfo) => {
        getters.userInfo(state);
    },
    // 菜单折叠
    setCollapseWidth: (state, collapseWidth) => {
        state.collapseWidth = collapseWidth;
    },
    // 改变全局风格
    setThemeCode: function (state, themeCode) {
        state.themeCode = themeCode.toString();
    },
    // 改变表格空态页图片路径
    setNoDataUrl: function (state, noDataUrl) {
        state.noDataUrl = noDataUrl;
    },
    // 改变logo图片路径
    setLogo: function (state, logoImg) {
        state.logoImg = logoImg;
    },
    //  当前url的一级菜单url
    setLevel1Url: function (state, url) {
        state.level1Url = url
    },
    // 记录菜单折叠收起按钮点击次数, 用于解决在首次展开菜单，出现横向滚动条问题
    setShowMenu: function (state, showMenu) {
        state.showMenu = showMenu;
    },
    // 当前页面按钮权限
    setBtnPower: (state, btnPower) => {
        state.btnPower = btnPower;
    },
    setTagsList: (state, tagsList) => {
        state.tagsList = tagsList;
    },
    //  是否隐藏视频
    setIsHideVideo: (state, isHideVideo) => {
        state.isHideVideo = isHideVideo
    },
    // 修改语言
    SET_LANGUAGE: (state, language) => {
        state.language = language
        console.log('store设置语言为：',language);
        setSessionStorageLanguage(language)
        //
        // let newLang=""
        // //
        // if (language=='zh-CN') {
        //     newLang = require("../lang/zh");
        // }else if(language=='en-US'){
        //     newLang = require("../lang/en");
        // }
        // console.log(122,Vuex.state);
        // i18n.setLocaleMessage(language,newLang.default)


        i18n.locale=language
        // Cookies.set('language', language)
    }
};
// 4. 异步修改数据
const actions = {
    setLanguage({ commit }, language) {
        commit('SET_LANGUAGE', language)
    },
};
// 5. 计算属性
const getters = {
    username: state => (state.username = localStorage.getItem("username")),
    userInfo: state => {
        let str = localStorage.getItem("userInfo");
        state.userInfo = eval("(" + str + ")");
        return state.userInfo;
    },
};
// 6. 导出store实例
export default new Vuex.Store({
    state,
    getters,
    actions,
    mutations,
});
